package com.itheima;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet("/historyServlet")
public class HistoryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 1.获取所有cookie
        Cookie[] cookies = request.getCookies();
        // 2.遍历
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if("history".equals(name)){
                response.getWriter().print("<a href=\"/goods.html\">您还没有浏览记录，去浏览吧</a>");
            }else {

                // 取值
                String value = cookie.getValue();
                String[] arr = value.split("_");
                // 将值响应给浏览器
                response.getWriter().print("您浏览的商品记录是："+ Arrays.toString(arr));
                response.getWriter().print("<br><a href=\"/goods.html\">继续浏览</a>");

            }
        }

    }
}
